package com.hjm.friend.aspect;


import com.hjm.common.core.constants.Constants;
import com.hjm.common.core.constants.SecurityConstants;
import com.hjm.common.core.enums.ResultCode;
import com.hjm.common.core.utils.ThreadLocalUtils;
import com.hjm.security.exception.ServiceException;
import com.hjm.friend.domain.user.User;
import com.hjm.friend.manage.UserCacheManager;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;


@Aspect
@Component
public class UserStatusCheckAspect {

    @Autowired
    private UserCacheManager userCacheManager;

    //配置织入点
    @Pointcut("@annotation(com.hjm.friend.annotation.CheckUserStatus)")
    public void aroundCut() {

    }

    @Before(value = "aroundCut()")
    public void before(JoinPoint point){
        Long userId = ThreadLocalUtils.get(Constants.USER_ID, Long.class);
        User user = userCacheManager.getUserById(userId);
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if (Objects.equals(user.getStatus(), Constants.FALSE)) {
            throw new ServiceException(ResultCode.FAILED_USER_BANNED);
        }
    }

}
